package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.TravelCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.locks.Condition;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepository travelRepository;

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(Travel strategy) {
        strategy.setId(null);
        travelRepository.save(strategy);
    }

    @Override
    public void update(Travel strategy) {
         travelRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
    travelRepository.deleteById(id);
    }

    @Override
    public Travel get(String id) {
        Travel travel = travelRepository.findById(id).get();
        if (StringUtils.hasLength(travel.getId())) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }
        return travel;
    }

    @Override
    public List<Travel> list() {
        return travelRepository.findAll();
    }


    @Override
    public Page<Travel> query(TravelQuery qo) {
        //查询对象
        Query query = new Query();
        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //构建出行天数条件
        TravelCondition condition = qo.getDay();
        if(condition!=null){
            query.addCriteria(Criteria.where("day").gte(condition.getMin()).lte(condition.getMax()));
        }

        //构建人均消费条件
        TravelCondition PerExCondition = qo.getPerEx();
        if(PerExCondition!=null){
            query.addCriteria(Criteria.where("perExpend").gte(PerExCondition.getMin()).lte(PerExCondition.getMax()));
        }

        Long totalCount = mongoTemplate.count(query, Travel.class);

        Pageable pageable=null;
        if(1==qo.getOrderType()){
            pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"viewnum");
        }else if (2==qo.getOrderType()){
            pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"createTime");
        }else{
            pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        }
        query.with(pageable);

        List<Travel> travels = mongoTemplate.find(query, Travel.class);
        //页面维护对象,页面需要作者相关信息显示
        for (Travel travel :travels) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }

        return new PageImpl(travels,pageable,totalCount);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        //通过destId查询目的地名字
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());
        //用户最后更新时间
        travel.setLastUpdateTime(new Date());
        if(StringUtils.hasLength(travel.getId())){
            this.update(travel);
        }else {
            //游记创建时间
            travel.setCreateTime(new Date());
            this.save(travel);
        }
    }

    @Override
    public void changeStateById(String id, int state) {
        //审核状态修改
        //如果游记无内容则不满足条件,不许修改
        Travel travel = this.get(id);
        if(travel.getContent()==null){
            throw new LogicException("内容不能为空，请重新审核");
        }
        //如果游记不是待审核状态。不允许审核
        if(travel.getState() != Travel.STATE_WAITING){
            throw new LogicException("游记当前状态不是待审核状态，不允许修改");
        }
        // 审核通过之后需要做什么
        if(state==Travel.STATE_RELEASE){
            //修改状态为审核通过状态
            travel.setState(Travel.STATE_RELEASE);
            //修改发布时间
            travel.setReleaseTime(new Date());
            //最后更新时间
            travel.setLastUpdateTime(new Date());
            this.update(travel);
        }else if (state == Travel.STATE_REJECT){
            //审核拒绝后需要修改的操作
            //1将状态修改为审核拒绝
            travel.setState(Travel.STATE_REJECT);
            //修改发布的时间
            travel.setReleaseTime(new Date());
            //修改最后更新时间
            travel.setLastUpdateTime(new Date());

        }







    }

    @Override
    public List<Travel> viewnumTop3(String id) {
        //展示点击量前三的目的地相关游记
        //查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("destId").is(id));
        //分页
        Pageable pageable = PageRequest.of(0,3,Sort.Direction.DESC,"viewnum");
        query.with(pageable);
        return mongoTemplate.find(query,Travel.class);

    }

    @Override
    public List<Travel> queryByDestName(String destname) {
        List<Travel> travels = travelRepository.queryByDestName(destname);
        for (Travel travel : travels) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }
        return travels;
    }


}
